package scheduledtasks.hander;



import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import scheduledtasks.annotations.Scheduled;
import scheduledtasks.annotations.ScheduledMethod;
import scheduledtasks.entity.TaskObj;


import java.lang.reflect.Method;


/**
 * 【BeanPostProcessor]
 *
 * @author tiny
 * @version 1.0
 * @company bc
 * @copyright (c) 2022 bcInc. All rights reserved.
 * @date 2022-11-08 16:00:26
 * @since  获取需要开启定时的对象
 */

public class SpringHander  implements BeanPostProcessor {





    @Autowired
    private SqlHander sqlHander;


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(null!=bean.getClass().getAnnotation(Scheduled.class)){
            Method[] methods= bean.getClass().getMethods();
            for (Method m:methods) {
                ScheduledMethod c=m.getAnnotation(ScheduledMethod.class);
                if(null!=c){
                     boolean flag=sqlHander.connectionisHave("select * from scheduled where class_name='"+beanName+"'"+" and metho_name='" +m.getName()+"'");
                     if(!flag){
                         sqlHander.uodate("INSERT INTO `scheduled`( `class_name`, `metho_name`,cron,sync,time) VALUES ('"+beanName+"'"+",'" +m.getName()+"','* * * * * * *',0,"+System.currentTimeMillis()+")");
                     }
                     TaskObj taskObj = new TaskObj();taskObj.setClassName(beanName);taskObj.setMethoName(m.getName());
                }
            }
        }
        return bean;
    }

























    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {





        return bean;
    }
}
